<template>
	<view>

	</view>
</template>

<script>
	export default {
		props: {
			fileModule: '',
		},
		data() {
			return {

			}
		},
		created() {

		},
		methods: {
			init(prop) {
				this.chooseImage(prop)
			},
			chooseImage(prop) {
				prop = prop || {}
				let count = prop.count || 1
				uni.chooseImage({
					count: count,
					sourceType: ['album', 'camera'],
					sizeType: ['compressed'],
					success: (res) => {
						if (res.tempFilePaths) {
							res.tempFilePaths.forEach(filePath => {

								// #ifdef H5
								prop.canvas = prop.canvas || 2
								prop.width = prop.width || 1200
								prop.height = prop.height || 1200

								this.canvasImage(filePath, prop.canvas, prop.width, prop.height, (res) => {
									this.doUpload(res)
								})
								// #endif

								// #ifndef H5
								this.doUpload(filePath)
								// #endif
								
							})
						}
					},
				})
			},
			doUpload(filePath) {
				uni.showLoading({
					title: '加载中'
				})
				this.$http.upload('/app/common/upload', {
					filePath: filePath,
					name: 'file'
				}).then(res => {
					uni.hideLoading()
					if (res && res.code && res.code == 200) {
						this.$emit('sync', res.fileName)
					}
				}).catch(err => {
					uni.hideLoading()
				})
			},
			canvasImage(src, canvas, width, height, callback) {
				let _canvas = document.createElement('canvas')
				let _image = new Image()
				_image.src = src
				_image.onload = function() {
					let _x = 0,
						_y = 0,
						_width = this.width,
						_height = this.height,
						_ratio

					if (canvas == 2) {
						// 1、只缩放不裁剪
						if (this.width > width || this.height > height) {
							_ratio = this.width / this.height
							if (_ratio > width / height) {
								_width = width
								_height = width / _ratio
							} else if (_ratio < width / height) {
								_width = height * _ratio
								_height = height
							}
						}

						// 画布大小
						_canvas.width = _width
						_canvas.height = _height
					} else {
						// 2、裁剪补齐到指定大小
						if (this.width >= width && this.height >= height) {
							_ratio = this.width / this.height
							if (_ratio >= width / height) {
								_width = _ratio * height
								_height = height
								_x = -(_width - width) / 2
							} else {
								_width = width
								_height = width / _ratio
								_y = -(_height - height) / 2
							}
						} else if (this.width > width && this.height < height) {
							_x = -(_width - width) / 2
							_y = (height - _height) / 2
						} else if (this.width < width && this.height > height) {
							_x = (width - _width) / 2
							_y = -(_height - height) / 2
						} else if (this.width < width && this.height < height) {
							_x = (width - _width) / 2
							_y = (height - _height) / 2
						}

						// 画布大小
						_canvas.width = width
						_canvas.height = height
					}

					let ctx = _canvas.getContext('2d')
					ctx.drawImage(_image, _x, _y, _width, _height)

					if (typeof callback == "function") {
						let base64 = _canvas.toDataURL("image/jpeg", 0.6) // image/jpeg image/png
						callback(base64)
					}
				}
			},
		}
	}
</script>
